# -*- coding: utf-8 -*-

import os
import logging

from loguru import logger
# from settings.conf import MyConf


class Loggers(object):

    def __new__(cls, *args, **kwargs):
        # 项目路径下创建log目录保存日志文件
        root_path = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
        log_path = os.path.join(root_path, "logs")  # 拼接指定路径
        # 判断目录是否存在，不存在则创建新的目录
        if not os.path.exists(log_path):
            os.makedirs(log_path)
        # filename = os.path.join(log_path, str(os.getpid()), 'xshare.log')
        filename = os.path.join(log_path, 'xshare.log')
        log_format = "{time:YYYY-MM-DD HH:mm:ss}|{elapsed}|{name} {file} " \
                     "{module} {function} line:{line} |{level}| {message}"
        logger.add(filename,  # 指定文件
                   colorize=True,
                   format=log_format,
                   encoding='utf-8',
                   retention='7 days',  # 设置历史保留时长
                   backtrace=True,  # 回溯
                   diagnose=True,  # 诊断
                   enqueue=True,  # 异步写入
                   rotation="00:00",
                   # 切割，设置文件大小，rotation="12:00"，rotation="1 week"
                   # filter="my_module",  # 过滤模块
                   compression="zip",  # 文件压缩
                   serialize=False
                   )
        return logger

    def trace(self, msg, *args, **kwargs):
        return logger.trace(msg, *args, **kwargs)

    def info(self, msg, *args, **kwargs):
        return logger.info(msg, *args, **kwargs)

    def debug(self, msg, *args, **kwargs):
        return logger.debug(msg, *args, **kwargs)

    def success(self, msg, *args, **kwargs):
        return logger.success(msg, *args, **kwargs)

    def warning(self, msg, *args, **kwargs):
        return logger.warning(msg, *args, **kwargs)

    def error(self, msg, *args, **kwargs):
        return logger.error(msg, *args, **kwargs)

    def exception(self, msg, *args, exc_info=True, **kwargs):
        return logger.exception(msg, *args, exc_info=True, **kwargs)

    def critical(self, msg, *args, exc_info=True, **kwargs):
        return logger.critical(msg, *args, exc_info=True, **kwargs)


# LOGFMT = '%(asctime)s.%(msecs)03d [%(process)d] %(levelname)-6s %(message)s'
# LOGDATEFMT = '%Y-%m-%d %H:%M:%S'
#
# LOGDIR = '/var/log/hsj'
# if not os.path.exists(LOGDIR):
#     try:
#         os.makedirs(LOGDIR)
#     except Exception as e:
#         LOGDIR = '/'
#
# LOGFILE = os.path.join(LOGDIR, 'dspt-api.log')
#
# logging.basicConfig(filename=LOGFILE,
#                     level=logging.INFO,
#                     format=LOGFMT,
#                     datefmt=LOGDATEFMT)
#
#
# def get_logger(logger_name):
#     return logging.getLogger(logger_name)


# if MyConf.ENV == "dev":
#     log = Loggers()
# else:
#     log = Loggers()
# log = get_logger(__name__)
log = Loggers()

if __name__ == '__main__':
    log.info('this is a info 中文')
    log.debug('this is a debug')

    def my_test():
        log.info("mutest")

    my_test()


